package com.inspur.project.common;

/**
 * @Description
 * @Date 2020/3/4 18:46
 * @auther SunJW
 */

import com.github.tobato.fastdfs.domain.fdfs.FileInfo;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.inspur.common.utils.StringUtils;
import com.inspur.common.utils.file.FastDFSClient;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.*;


@RestController
@RequestMapping("/fdfs")
public class FastDFSController {

    @Autowired
    private FastDFSClient fdfsClient;

    /**
     * 文件上传
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    public Map<String, Object> upload(MultipartFile file) throws Exception{

        String url = fdfsClient.uploadFile(file);

        Map<String,Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "上传成功");
        result.put("url", url);

        return result;
    }

    /**
     * 文件上传
     * @param fileList
     * @return
     * @throws Exception
     */
    @RequestMapping("/uploadFiles")
    public Map<String, Object> uploadFiles(MultipartFile[] fileList) throws Exception{

        List<String> urlList = new ArrayList<>();
        for(MultipartFile file : fileList) {
            String url = fdfsClient.uploadFile(file);
            urlList.add(url);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "上传成功");
        result.put("url", String.join(",", urlList));

        return result;
    }

    /**
     * 文件下载
     * 示例：group1/M00/00/00/wKj0gl02aeqAKqoAAABWsOuJo1w347.jpg
     * @param fileUrl  url 开头从组名开始
     * @param response
     * @throws Exception
     */
    @RequestMapping("/download")
    public void download(String fileUrl, HttpServletResponse response) throws Exception{

        byte[] data = fdfsClient.download(fileUrl);

        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("test.jpg", "UTF-8"));

        // 写出
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.write(data, outputStream);
    }

    /**
     * 删除文件
     * 示例：group1/M00/00/00/wKj0gl02aeqAKqoAAABWsOuJo1w347.jpg
     * @param fileUrl
     * @return
     */
    @RequestMapping(value = "/deleteFile", method = RequestMethod.POST)
    public Map<String,Object> deleteFile(@RequestParam("fileUrl") String fileUrl) {
        Map<String, Object> result = new HashMap<>();

        try {
            fdfsClient.deleteFile(fileUrl);
            result.put("code", 200);
            result.put("msg", "删除成功");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "删除失败");
            result.put("detail", e.getMessage());
        }
        return result;
    }

    /**
     * 删除文件，文件路径用英文逗号隔开
     * 示例：group1/M00/00/00/wKj0gl02aeqAKqoAAABWsOuJo1w347.jpg
     * @param fileUrl
     * @return
     */
    @RequestMapping(value = "/deleteFiles", method = RequestMethod.POST)
    public Map<String,Object> deleteFiles(@RequestParam("fileUrl") String fileUrl) {
        Map<String, Object> result = new HashMap<>();
        if(StringUtils.isEmpty(fileUrl)){
            result.put("code", 500);
            result.put("msg", "文件路径为空");
            return result;
        }
        try {
            String[] urls = fileUrl.split(",");
            fdfsClient.deleteFiles(urls);
            result.put("code", 200);
            result.put("msg", "删除成功");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "删除失败");
            result.put("detail", e.getMessage());
        }
        return result;
    }

    /**
     * 删除图片及缩略图文件，文件路径用英文逗号隔开
     * 示例：group1/M00/00/00/wKj0gl02aeqAKqoAAABWsOuJo1w347.jpg
     * @param fileUrl
     * @return
     */
    @RequestMapping(value = "/deleteImageWithThumbImage", method = RequestMethod.POST)
    public Map<String,Object> deleteImageWithThumbImage(@RequestParam("fileUrl") String fileUrl) {
        Map<String, Object> result = new HashMap<>();
        if(StringUtils.isEmpty(fileUrl)){
            result.put("code", 500);
            result.put("msg", "文件路径为空");
            return result;
        }
        try {
            String[] urls = fileUrl.split(",");
            fdfsClient.deleteImageWithThumbImage(urls);
            result.put("code", 200);
            result.put("msg", "删除成功");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "删除失败");
            result.put("detail", e.getMessage());
        }
        return result;
    }

    /**
     * 获取文件信息
     *  示例入参：groupName:group1，path:M00/00/00/wKj0gl0xijOABpueAABlGHvbGSg492.jpg
     * @param fileUrl
     * @return
     */
    @RequestMapping(value = "/getFile", method = RequestMethod.POST)
    public FileInfo getFile(@RequestParam("fileUrl") String fileUrl) {
        FileInfo fileInfo = fdfsClient.getFile(fileUrl);
        return fileInfo;
    }

    /**
     * 获取文件元信息
     *
     * @param fileUrl
     * @return
     */
    @RequestMapping(value = "/getMetadata", method = RequestMethod.POST)
    public Set<MetaData> getMetadata(@RequestParam("fileUrl") String fileUrl) {
        Set<MetaData> metaData = fdfsClient.getMetadata(fileUrl);
        return metaData;
    }

    /**
     * 上传文件并生成缩略图
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadImageAndCrtThumbImage", method = RequestMethod.POST)
    public Map<String,Object> uploadImageAndCrtThumbImage(@RequestPart("file") MultipartFile file) {
        Map<String,Object> result = new HashMap<>();

        String url = fdfsClient.uploadImageAndCrtThumbImage(file);

        result.put("code", 200);
        result.put("msg", "上传成功");
        result.put("url", url);
        return result;
    }

    /**
     * 多图片上传并生成缩略图
     * @param fileList
     * @return
     * @throws Exception
     */
    @RequestMapping("/uploadImageAndCrtThumbImageList")
    public Map<String, Object> uploadImageAndCrtThumbImageList(MultipartFile[] fileList) throws Exception{

        List<String> urlList = new ArrayList<>();
        for(MultipartFile file : fileList) {
            String url = fdfsClient.uploadImageAndCrtThumbImage(file);
            urlList.add(url);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "上传成功");
        result.put("url", String.join(",", urlList));

        return result;
    }

}
